package com.myk.game.gobangchess.rooms.absrooms.movechess;

import com.myk.game.gobangchess.Room;
import com.myk.game.gobangchess.rooms.absrooms.AbsMultiViewChessRoom;
import com.myk.game.gobangchess.signs.ColorSign;
import com.myk.game.gobangchess.signs.GroundSign;
import com.myk.game.gobangchess.signs.TextSign;

import java.util.ArrayList;
import java.util.List;

/**
 * 走子棋，棋类定义
 */
public interface MovedChess {
    int getRow();
    int getCol();
    void moveTo(int row, int col);
    default int getIndex(Room room) {
        return room.getIndex(getRow(), getCol());
    }
    int getSide();
    String getName();
    default String getColor() {
        return getSide() == 1 ? MoreColorCost.BLACK_CHESS_WORD : MoreColorCost.WHITE_CHESS_WORD;
    }
    List<Integer> getMoveRange(MovedChessRoom room);
    List<Integer> getEatRange(MovedChessRoom room);
    default void render(MovedChessRoom room, boolean selected) {
        room.getCurGrids()[getRow()][getCol()] = getSide(); //显示棋子的底座
        room.getSignList().add(new TextSign(room.getIndex(getRow(), getCol()), getName(), getColor()));
        if (selected) {
            //画一下选中框
            room.getSignList().add(new GroundSign(room.getIndex(getRow(), getCol()), MoreColorCost.NO_COLOR, MoreColorCost.SELECT_ED, 2, 0, 0));
            //画一下可移动范围
            for (Integer index : getMoveRange(room)) {
                room.getSignList().add(new ColorSign(index, MoreColorCost.MOVE));
            }
            //画一下吃子范围
            for (Integer index : getEatRange(room)) {
                room.getSignList().add(new ColorSign(index, MoreColorCost.EAT));
            }
        }
    }

    default List<Integer> genMoveRange(MovedChessRoom room, int[][] dxdys, int distance) {
        return genMoveRange(room, dxdys, distance, 0, 0);
    }

    default List<Integer> genMoveRange(MovedChessRoom room, int[][] dxdys, int distance, int canJump, int mustJump) {
        List<Integer> rangeIndex = new ArrayList<>();
        for (int[] dxdy : dxdys) {
            int skip = 0;
            for (int x = getRow() + dxdy[0], y = getCol() + dxdy[1], i = 0; x >= 0 && x < room.getCurGrids().length && y >= 0 && y < room.getCurGrids()[0].length && i < distance; x += dxdy[0], y += dxdy[1], i++) {
                MovedChess chess = room.getChessByPos(x, y);
                if (chess != null) {
                    skip++;
                    if (skip > canJump) {
                        break;
                    }
                    continue;
                }
                if (skip >= mustJump) {
                    rangeIndex.add(room.getIndex(x, y));
                }
            }
        }
        return rangeIndex;
    }

    default List<Integer> genEatRange(MovedChessRoom room, int[][] dxdys, int distance) {
        return genEatRange(room, dxdys, distance, 0, 0);
    }

    default List<Integer> genEatRange(MovedChessRoom room, int[][] dxdys, int distance, int canJump, int mustJump) {
        List<Integer> rangeIndex = new ArrayList<>();
        for (int[] dxdy : dxdys) {
            int skip = 0;
            for (int x = getRow() + dxdy[0], y = getCol() + dxdy[1], i = 0; x >= 0 && x < room.getCurGrids().length && y >= 0 && y < room.getCurGrids()[0].length && i < distance; x += dxdy[0], y += dxdy[1], i++) {
                MovedChess chess = room.getChessByPos(x, y);
                if (chess != null) {
                    if (chess.getSide() != getSide() && skip >= mustJump) {
                        rangeIndex.add(room.getIndex(x, y));
                    }
                    skip++;
                    if (skip > canJump) {
                        break;
                    }
                }
            }
        }
        return rangeIndex;
    }

}
